// 参考 https://juejin.im/post/5ad13d8a6fb9a028ce7c0721
console.time('idle')
function timeSlicing (gen) {
  return new Promise((resolve, reject) => {
    if (typeof gen === 'function') gen = gen()
    if (!gen || typeof gen.next !== 'function') return
    ;(function next () {
      const res = gen.next()
      if (res.done) {
        resolve()
        return
      }
      // 效率对比requestAnimationFrame最合理 , setTimeout 0最快,最耗资源,requestIdleCallback最省资源但刷新频率较低
      // requestIdleCallback(next) // 174554.125ms //  5.72 /1s low
      // requestIdleCallback(next,{timeout:0}) //60220.820068359375ms // 16.59 /1s
      //setTimeout(next, 0) //  5093.2001953125ms / 196/1s High
      requestAnimationFrame(next) // 16649.630859375ms  60/1s  middle , default
      // 直接循环一千次为37ms ,  27027次 /1s  //最快 最耗费资源
    })()
  })
}

// 调用时间分片函数
timeSlicing(function* () {
  // const start = performance.now()
  var a = 0
  while (a < 1000) {
    console.log('执行逻辑')
    a++
    yield
  }
  console.log('done')
  console.timeEnd('idle')
})
  .then((result) => {
    console.log('完成了  做点什么')
  })
  .catch((err) => {})
